<?php
namespace Admin\Model;
use Think\Model\RelationModel;

class CourseModel extends RelationModel {

	protected $_validate = array(
		array('id', 'require', '课程id不能为空！'),
		array('major_id', 'require', '专业不能为空！', 1),
		array('name', 'require', '名称不能为空！'),
		array('type', 'require', '课程类型不能为空！'),
	);

	protected $_link = array(
		'major'	=> array(
			'mapping_type'	=> self::BELONGS_TO,
			'class_name'	=> 'Major',
			'foreign_key'	=> 'major_id',
		),
		'logic_classes'	=> array(
		    'mapping_type'  => self::HAS_MANY,
		    'class_name'    => 'LogicClass',
		    'foreign_key'   => 'course_id',
		),
	);

	public function checkUnique($course) {
		$map['major_id'] = $course['major_id'];
		$map['name'] = $course['name'];
		$map['type'] = (int)$course['type'];
		return !M('Course')->where($map)->find();
	}

	public function hasChildObj($id) {
		$result = $this->where(array('id' => $id))->relation('logic_classes')->find();
		if($result['logic_classes']) {
			$this->error = '还有班级在教授这门课程，请先删除这些逻辑班级！';
			return true;
		}
		return false;
	}

}
